package com.example.benefitcommunity.service.impl;

import com.example.benefitcommunity.entity.BcNotificationAnnouncement;
import com.example.benefitcommunity.entity.BcServiceDemands;
import com.example.benefitcommunity.dao.BcServiceDemandsDao;
import com.example.benefitcommunity.entity.Result;
import com.example.benefitcommunity.enums.StatusCode;
import com.example.benefitcommunity.service.BcServiceDemandsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务诉求表(BcServiceDemands)表服务实现类
 *
 * @author makejava
 * @since 2025-08-21 09:58:37
 */
@Transactional(rollbackFor = Exception.class)
@Service("bcServiceDemandsService")
public class BcServiceDemandsServiceImpl implements BcServiceDemandsService {
    @Resource
    private BcServiceDemandsDao bcServiceDemandsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BcServiceDemands queryById(Integer id) {
        return this.bcServiceDemandsDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param bcServiceDemands 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<BcServiceDemands> queryByPage(BcServiceDemands bcServiceDemands, PageRequest pageRequest) {
        long total = this.bcServiceDemandsDao.count(bcServiceDemands);
        return new PageImpl<>(this.bcServiceDemandsDao.queryAllByLimit(bcServiceDemands, pageRequest), pageRequest, total);
    }

    /**
     * @param type        姓名
     * @param communityid
     * @param pagenum     页码
     * @param pagesize    页大小
     * @return
     */
    @Override
    public Result<Object> queryByServiceDemands(String type, Integer communityid, Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum, pagesize);
        BcServiceDemands bcServiceDemands = new BcServiceDemands();
        bcServiceDemands.setType(type);
        bcServiceDemands.setCommunityid(communityid);
        List<BcServiceDemands> list = this.bcServiceDemandsDao.queryAll(bcServiceDemands);
        PageInfo<BcServiceDemands> pageInfo = new PageInfo<>(list);
        return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * @param type 类型
     * @return
     */
    @Override
    public Result<Object> queryByType(String type) {
        Map<String, Object> map = new HashMap<>();
        BcServiceDemands bcServiceDemands = new BcServiceDemands();
        bcServiceDemands.setType(type);
        List<BcServiceDemands> list = this.bcServiceDemandsDao.queryAll(bcServiceDemands);
        map.put("ServiceDemands", list);
        map.put("module", "服务诉求");
        return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), (long) list.size(), map);
    }

    /**
     * 新增数据
     *
     * @param bcServiceDemands 实例对象
     * @return 实例对象
     */
    @Override
    public Result<Object> insert(BcServiceDemands bcServiceDemands) {
        List<BcServiceDemands> list = this.bcServiceDemandsDao.queryAll(bcServiceDemands);
        if (list.isEmpty()) {
            if (this.bcServiceDemandsDao.insert(bcServiceDemands) > 0) {
                return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), 1L, "服务诉求新增成功！");
            }
            return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "服务诉求新增失败！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "服务诉求已存在！");
    }

    /**
     * 修改数据
     *
     * @param bcServiceDemands 实例对象
     * @return 实例对象
     */
    @Override
    public Result<Object> update(BcServiceDemands bcServiceDemands) {
        BcServiceDemands bcServiceDemandsNew = this.bcServiceDemandsDao.queryById(bcServiceDemands.getId());
        if (bcServiceDemandsNew != null) {
            if (this.bcServiceDemandsDao.update(bcServiceDemands) > 0) {
                return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), 1L, "服务诉求更新成功！");
            }
            return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "服务诉求更新失败！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "服务诉求不存在！");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Result<Object> deleteById(Integer[] id) {
        int count = 0;
        //删除个别
        for (Integer i : id) {
            //删除单个
            if (this.bcServiceDemandsDao.deleteById(i) > 0) {
                count++;
            }
        }
        //判断删除结果
        if (count == id.length) {
            return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), (long) id.length, "服务诉求删除成功！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "服务诉求删除失败！");
    }
}
